/*    */ package com.zimbra.qa.unittest;
/*    */ 
/*    */ import com.zimbra.common.net.ProxySelectors;
/*    */ import com.zimbra.common.net.SocketFactories;
/*    */ import java.io.IOException;
/*    */ import java.io.InputStream;
/*    */ import java.io.PrintStream;
/*    */ import java.net.InetSocketAddress;
/*    */ import java.net.Proxy;
/*    */ import java.net.Proxy.Type;
/*    */ import java.net.ProxySelector;
/*    */ import java.net.Socket;
/*    */ import java.net.SocketAddress;
/*    */ import java.net.URI;
/*    */ import java.net.URL;
/*    */ import java.net.URLConnection;
/*    */ import java.util.Arrays;
/*    */ import java.util.List;
/*    */ import javax.net.SocketFactory;
/*    */ import junit.framework.TestCase;
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ public class TestSocks
/*    */   extends TestCase
/*    */ {
/*    */   private static final String PROXY_HOST = "localhost";
/*    */   private static final int PROXY_PORT = 1080;
/* 39 */   private static final InetSocketAddress PROXY_ADDR = new InetSocketAddress("localhost", 1080);
/*    */   
/*    */   private static final String HTTP_URL = "http://www.news.com";
/*    */   
/*    */   private static final String HTTPS_URL = "https://www.news.com";
/*    */   private static final int LOCAL_PORT = 9999;
/*    */   
/*    */   static
/*    */   {
/* 48 */     SocketFactories.registerProtocols();
/*    */   }
/*    */   
/*    */   public void testConnect() throws IOException {
/* 52 */     connect(new SimpleProxySelector(null));
/* 53 */     connect(ProxySelectors.defaultProxySelector());
/*    */   }
/*    */   
/*    */   public void testSystemProxy() throws Exception {
/* 57 */     ProxySelector ps = ProxySelectors.nativeProxySelector();
/* 58 */     assertNotNull(ps);
/* 59 */     List<Proxy> proxies = ps.select(new URI("socket://www.vmware.com"));
/* 60 */     assertEquals(1, proxies.size());
/* 61 */     Proxy proxy = (Proxy)proxies.get(0);
/* 62 */     assertEquals(Proxy.Type.SOCKS, proxy.type());
/* 63 */     assertEquals(PROXY_ADDR, proxy.address());
/*    */   }
/*    */   
/*    */   public void testHttpProxy() throws Exception {
/* 67 */     ProxySelector ps = ProxySelectors.defaultProxySelector();
/* 68 */     List<Proxy> proxies = ps.select(new URI("http://www.news.com"));
/* 69 */     for (Proxy proxy : proxies) {
/* 70 */       System.out.println("proxy = " + proxy);
/*    */     }
/* 72 */     URLConnection conn = new URL("https://www.news.com").openConnection();
/* 73 */     conn.setConnectTimeout(3000);
/* 74 */     conn.connect();
/* 75 */     conn.getInputStream().read();
/*    */   }
/*    */   
/*    */   private Socket connect(ProxySelector ps) throws IOException {
/* 79 */     SocketFactory sf = SocketFactories.proxySelectorSocketFactory(ps);
/* 80 */     Socket sock = sf.createSocket();
/* 81 */     assertFalse(sock.isConnected());
/* 82 */     assertFalse(sock.isBound());
/* 83 */     sock.bind(new InetSocketAddress(0));
/* 84 */     sock.connect(new InetSocketAddress("www.news.com", 80));
/* 85 */     assertTrue(sock.isConnected());
/* 86 */     return sock;
/*    */   }
/*    */   
/*    */   private static class SimpleProxySelector extends ProxySelector
/*    */   {
/*    */     public List<Proxy> select(URI uri) {
/* 92 */       return Arrays.asList(new Proxy[] { new Proxy(Proxy.Type.SOCKS, TestSocks.PROXY_ADDR) });
/*    */     }
/*    */     
/*    */     public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {}
/*    */   }
/*    */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestSocks.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */